<template>
    <el-dialog @close="cancleAddDict" v-model="dialogVisible" width="30%" title="新增检测项信息" destroy-on-close center>
        <template #default>
            <el-form :model="dict" label-position="right" label-width="100px">

              <el-form-item label="*检测项名称">
                <el-input  v-model="dict.checkItemName" placeholder="请输入检测项名称" />
              </el-form-item>

                <el-form-item label="*检测项类型">
                  <el-input
                      v-model="dict.checkItemType"
                      style="max-width: 600px"
                      placeholder="请选择检测项"
                  >
                    <template #append>
                      <el-button icon="el-icon-search" @click="findcheckItem"></el-button>
                    </template>
                  </el-input>
                </el-form-item>

                <el-form-item label="检测工具">
                    <el-input  v-model="dict.checkItemTools" placeholder="请输入检测工具" />
                </el-form-item>
              <el-form-item label="检测要求">
                <el-input  type="textarea" v-model="dict.checkRequest" placeholder="请输入内容" />
              </el-form-item>
              <el-form-item label="标准值">
                <el-input  v-model="dict.checkItemStandard" placeholder="请输入标准值" />
              </el-form-item>

              <el-form-item label="单位">
                <el-select  v-model="dict.checkItemUnit"  >
                  <el-option v-for="item in types" :key="item.checkItemUnit" :label="item.checkItemUnit" :value="item.checkItemUnit" />
                </el-select>
              </el-form-item>

              <el-form-item label="误差上限">
                  <el-input-number v-model="up" :min="0" :max="10" :step="0.1" @change="handleChangeUp" />
              </el-form-item>

              <el-form-item label="误差下限">
                  <el-input-number v-model="down" :min="-10" :max="0" :step="0.1" @change="handleChangeDown" />
              </el-form-item>


              <el-form-item label="说明图">
                <el-input  v-model="dict.notePicture" placeholder="请输入说明图" />
              </el-form-item>

              <el-form-item label="备注">
                <el-input type="textarea" v-model="dict.note" placeholder="请输入内容" />
              </el-form-item>

            </el-form>
        </template>
        <!-- footer插槽中放两个按钮 -->
        <template #footer>
            <el-row>
                <el-col :span="12">
                    <el-button type="primary" @click="addDict">确定</el-button>
                </el-col>
                <el-col :span="12">
                    <el-button type="primary" @click="cancleAddDict">取消</el-button>
                </el-col>
            </el-row>
        </template>
    </el-dialog>
</template>
<script>
import { watch } from "vue";
// 引入我们申明的dict的api的js
import FlawItemRequest from "@/api/flawItem.js";
import axios from 'axios';
export default {
  // components:{
  //   MyComponent,
  // },
    props: ["visible","checkTemplateId"],
    emits: ["optionCompleted"], // 对外暴露一个事件，用来传递添加学生的操作结果
    data() {
        return {
            dict: {
              checkTemplateId:0,
              checkItemId:0,
              checkItemType:undefined,
              checkItemTools:undefined,
              checkRequest:undefined,
              checkItemStandard:undefined,
              checkItemUnit:undefined,
              checkItemErrorUp:0,
              checkItemErrorDown:0,
              notePicture:undefined,
              note:undefined,
            },// 绑定新增的字典信息
            dialogVisible: false,// 添加字典窗口的显示与隐藏
            types:{},
            isAutoGenerate:false,
            up:0,
            down:0,

          CHECK_No:'CHECK_NO',
        }
    },
     methods: {
       /**
        * 加载检测项所有信息
        */
       loadCheckDictAll(){
         FlawItemRequest.loadFlawItemAll(res => {
           const types = res.data.map(item => {
             return {
               checkItemUnit: item.checkItemUnit,
             }
           })
           let seen = {};
           let filteredArr = types.filter(item => {
             let key = item.checkItemUnit ;
             if (!seen[key]) {
               seen[key] = true;
               return true;
             }
             return false;
           });
           this.types = filteredArr;
         })
       },


       /**
        * 误差上限
        */
       handleChangeUp(value){
           this.dict.checkItemErrorUp = value;
       },
       /**
        * 误差下限
        */
       handleChangeDown(value){
         this.dict.checkItemErrorDown = value;
       },

       /**
        * 检测项中选择检测项
        */
       findcheckItem(){

       },
        /**
         * 取消添加学生的事件
         */
        cancleAddDict(){
            this.dict = {};
            // 通知父组件  操作完成，让父组件关闭窗体
            this.$emit("optionCompleted");
        },
        /**
         * 添加信息
         */
        addDict(){
          this.dict.checkItemId = this.dict.checkItemId;
          this.dict.checkTemplateId = this.checkTemplateId;
          console.log(this.dict.checkTemplateId);
            FlawItemRequest.addFlawItemTemplate(this.dict,res=>{
                if (res.code == '200'){
                    this.$message({
                        type: 'success',
                        message: '添加成功!'
                    });
                    // 通知父组件  操作完成，让父组件关闭窗体
                    this.$emit("optionCompleted");
                }else if(res.code == '300'){
                    this.$message({
                        type: 'warning',
                        message: '字典' + (res.msg.indexOf("checkItemName") > 0 ?"名称":"类型") + "重复"
                    });
                }else{
                    this.$message({
                        type: 'error',
                        message: '添加字典失败!'
                    });
                }
            });
        },


    },computed:{
    /**
     * 编码自动生成
     * @returns {Promise<axios.AxiosResponse<any>>|*}
     */
    get(){
      return this.isAutoGenerate ? axios.get('http://localhost:8086/md/rules/query', {
        params: {
          rulesName: 'CHECK_NO'
        }
      }) .then(response => {
            // 处理axios返回的数据
            this.dict.checkItemId = response.data.checkItemId; // 假设返回的数据是编码值
          }).catch(error => {
            console.error('请求出错：', error);
          })
          : this.dict.checkItemId;
    },
    set(value) {
      this.dict.checkItemId = value;
    },

  }, mounted(props) {
    this.loadCheckDictAll();
        // 侦听props   一旦发生变化，就会出发下面的函数
        watch(() => this.visible, (newVal, oldVal) => {
            // 将最新的值设置给弹窗的dialogVisible属性
            this.dialogVisible = newVal;
        });
    }
}
</script>